package de.idgard.android.addon.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndEntry;

import de.idgard.android.R;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.android.addon.ui.services.BrowserTabService;
import de.idgard.android.addon.ui.services.LayoutAdjustmentService;
import de.idgard.android.addon.ui.services.NewsService;
import de.idgard.android.addon.ui.services.PropertiesService;
import de.idgard.android.addon.ui.services.impl.UIServiceRegistry;
import de.idgard.json.transport.Receiver;

/**
 * @author pratik popat - This class shows dialog of recent IDGard news.
 * 
 */
public class IDGardNewsDialog extends Dialog {

	private BrowserTabService getBrowserTabService() {
		return GlobalServiceRegistry.instance().getBrowserTabService();
	}

	private NewsService getNewsService() {
		return GlobalServiceRegistry.instance().getNewsService();
	}

	private PropertiesService getPropertiesService() {
		return GlobalServiceRegistry.instance().getPropertiesService();
	}

	private LayoutAdjustmentService getLayoutAdjustmentService() {
		return UIServiceRegistry.instance().getlayouAdjustmentService();
	}

	private Button btnClose;
	private TextView tvTitle;
	private WebView tvDesc;
	private TextView tvMore;
	private ProgressBar pbDialogNews;
	private String moreUrl;

	public IDGardNewsDialog(Context context) {
		super(context);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.idgard_dialog_news);
		int width = getLayoutAdjustmentService().getScreenWidth() * 90 / 100;
		getLayoutAdjustmentService().adjustDialogLayout(this,
				android.view.WindowManager.LayoutParams.WRAP_CONTENT, width,
				-1, -1);

		tvTitle = (TextView) findViewById(R.id.tv_news_title);
		tvMore = (TextView) findViewById(R.id.tv_news_more);
		tvDesc = (WebView) findViewById(R.id.tv_news_desc);
		btnClose = (Button) findViewById(R.id.newsCloseBtn);
		pbDialogNews = (ProgressBar) findViewById(R.id.pb_dialog_news);

		pbDialogNews.setVisibility(View.VISIBLE);

		getNewsService().getLatestNews().fire(new Receiver<SyndEntry>() {
			@Override
			public void onSuccess(SyndEntry entry) {
				if (entry != null) {
					String title = entry.getTitle();
					if (title != null && !title.equals("")) {
						SpannableString titleU = new SpannableString(title);
						titleU.setSpan(new UnderlineSpan(), 0, title.length(),
								0);

						tvTitle.setText(titleU);
						if (entry.getDescription() != null) {
							tvDesc.loadDataWithBaseURL(
									"fake://this.is.fake.url", entry
											.getDescription().getValue(),
									"text/html", "UTF-8", null);
							moreUrl = entry.getLink();
						}
					} else {
						tvTitle.setText(getPropertiesService().getString(
								R.string.err_news_loading));
						tvDesc.loadData("", "text/html", "UTF-8");
					}
				}
				pbDialogNews.setVisibility(View.INVISIBLE);
			}
		});
		tvMore.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getBrowserTabService().addNewTab();
				GlobalServiceRegistry.instance().getZircoToIDGardAPI()
						.navigateToUrl(moreUrl);
				dismiss();
				pbDialogNews.setVisibility(View.INVISIBLE);
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
				pbDialogNews.setVisibility(View.INVISIBLE);
			}
		});

	}
}
